package com.yunwaikeji.gy_tool_library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYLoadingInfo;
import com.yunwaikeji.gy_tool_library.view.GYLinearLayout;

/**
 * 等待View
 * 郭杨
 * 2019/07/07
 */
public class GYLoadingLayout extends GYLinearLayout implements GYLoadingInfo {

    /**
     * 设置内容view
     *
     * @param contentView
     * @return
     */
    public GYLoadingLayout setContentView(View contentView) {
        contentView.setTag(ShowView.CONTENT);
        removeView(ShowView.CONTENT);
        addView(contentView);
        return show(showView);
    }

    /**
     * 获取内容view
     *
     * @return
     */
    public View getContentView() {
        return getView(ShowView.CONTENT);
    }

    public View getView(@ShowView int showView) {
        for (View view : getViewSet().getChildViews()) {
            if ((int) view.getTag() == showView) {
                return view;
            }
        }
        return null;
    }

    /**
     * 显示等待view
     *
     * @return
     */
    public GYLoadingLayout showLoading() {
        showLoading("加载中...");
        return this;
    }

    /**
     * 显示等待view
     *
     * @param text
     * @return
     */
    public GYLoadingLayout showLoading(String text) {
        loadingTextView.setText(text);
        show(0);
        return this;
    }

    /**
     * 设置等待view
     *
     * @param loadingView
     * @return
     */
    public GYLoadingLayout setLoadingView(View loadingView) {
        loadingView.setTag(ShowView.LOADING);
        removeView(ShowView.LOADING);
        addView(loadingView);
        return show(showView);
    }

    /**
     * 设置为空view
     *
     * @param emptyView
     * @return
     */
    public GYLoadingLayout setEmptyView(View emptyView) {
        emptyView.setTag(ShowView.EMPTY);
        removeView(ShowView.EMPTY);
        addView(emptyView);
        return show(showView);
    }

    /**
     * 设置状态view
     *
     * @param stateView
     * @return
     */
    public GYLoadingLayout setStateView(View stateView) {
        stateView.setTag(ShowView.STATE);
        removeView(ShowView.STATE);
        addView(stateView);
        return show(showView);
    }

    /**
     * 设置错误view
     *
     * @param errorView
     * @return
     */
    public GYLoadingLayout setErrorView(View errorView) {
        errorView.setTag(ShowView.ERROR);
        removeView(ShowView.ERROR);
        addView(errorView);
        return show(showView);
    }

    /**
     * 设置为空文字点击事件
     *
     * @param onEmptyTextClickListener
     * @return
     */
    public GYLoadingLayout setOnEmptyTextClickListener(OnClickListener onEmptyTextClickListener) {
        emptyTextView.setOnClickListener(onEmptyTextClickListener);
        return this;
    }

    /**
     * 显示为空view
     *
     * @return 回调
     */
    public GYLoadingLayout showEmpty() {
        show(ShowView.EMPTY);
        return this;
    }

    /**
     * 显示为空view
     *
     * @param text 文字
     * @return 回调
     */
    public GYLoadingLayout showEmpty(String text) {
        emptyTextView.setText(text);
        showEmpty();
        return this;
    }

    /**
     * 设置状态文字点击事件
     *
     * @param onStateTextClickListener
     * @return
     */
    public GYLoadingLayout setOnStateTextClickListener(OnClickListener onStateTextClickListener) {
        stateTextView.setOnClickListener(onStateTextClickListener);
        return this;
    }

    /**
     * 设置错误文字点击事件
     *
     * @param onErrorTextClickListener 点击
     * @return 回调
     */
    public GYLoadingLayout setOnErrorTextClickListener(OnClickListener onErrorTextClickListener) {
        errorTextView.setOnClickListener(onErrorTextClickListener);
        return this;
    }

    /**
     * 显示状态view
     *
     * @param text 文字
     * @return 回调
     */
    public GYLoadingLayout showState(String text) {
        stateTextView.setText(text);
        showState();
        return this;
    }

    public GYLoadingLayout showState() {
        show(ShowView.STATE);
        return this;
    }

    /**
     * 显示内容view
     *
     * @return回调
     */
    public GYLoadingLayout showContent() {
        show(ShowView.CONTENT);
        return this;
    }

    /**
     * 显示错误view
     *
     * @return 回调
     */
    public GYLoadingLayout showError() {
        show(ShowView.ERROR);
        return this;
    }

    public GYLoadingLayout setErrorText(String text) {
        if (errorTextView != null) {
            errorTextView.setText(text);
        }
        return this;
    }

    /**
     * 显示错误view
     *
     * @param text 文字
     * @return 回调
     */
    public GYLoadingLayout showError(String text) {
        setErrorText(text);
        showError();
        return this;
    }

    /**
     * 显示自定义view
     *
     * @param position content view位置
     * @return 回调
     */
    public GYLoadingLayout showContentView(int position) {
        show(position + count);
        return this;
    }

    @ShowView
    public int getShowView() {
        return showView;
    }

    public GYLoadingLayout(Context context) {
        super(context);
        init(null);
    }

    public GYLoadingLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public GYLoadingLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    @SuppressLint("NewApi")
    public GYLoadingLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    @Override
    public void addView(View child) {
        child.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        super.addView(child);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        }
    }

    private TextView loadingTextView;
    private TextView emptyTextView;
    private TextView stateTextView;
    private TextView errorTextView;

//    private LinearLayout loadingLL;
//    private LinearLayout emptyLL;
//    private LinearLayout stateLL;
//    private LinearLayout errorLL;

    private int count = 0;

    private TextView textView() {
        TextView textView = new TextView(getContext());
        textView.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        return textView;
    }

    private LinearLayout linearLayout() {
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setBackgroundColor(0xFFFFFF);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        return linearLayout;
    }

    private void init(AttributeSet attributeSet) {
//        setOrientation(LinearLayout.VERTICAL);
//        loadingLL = linearLayout();
//        emptyLL = linearLayout();
//        stateLL = linearLayout();
//        errorLL = linearLayout();

        loadingTextView = textView();
        loadingTextView.setText("加载中，请稍后...");
        emptyTextView = textView();
        emptyTextView.setText("数据为空...");
        stateTextView = textView();
        stateTextView.setText("状态...");
        errorTextView = textView();
        errorTextView.setText("网络错误...");

//        loadingLL.addView(loadingTextView);
//        emptyLL.addView(emptyTextView);
//        stateLL.addView(stateTextView);
//        errorLL.addView(errorTextView);

        setLoadingView(loadingTextView);
        setEmptyView(emptyTextView);
        setStateView(stateTextView);
        setErrorView(errorTextView);

        count = getChildCount();

        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYLoadingLayout);
            show(typedArray.getInt(R.styleable.GYLoadingLayout_showView, showView));
            typedArray.recycle();
        }
    }

    private int showView = ShowView.LOADING;

    public GYLoadingLayout show(@ShowView int showView) {
        this.showView = showView;
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view.getTag() == null || (int) view.getTag() == this.showView) {
                view.setVisibility(VISIBLE);
            } else {
                view.setVisibility(GONE);
            }
        }
        return this;
    }

    private GYLoadingLayout removeView(@ShowView int removeView) {
        for (int i = getChildCount() - 1; i >= 0; i--) {
            View view = getChildAt(i);
            if ((int) view.getTag() == removeView) {
                removeViewAt(i);
            }
        }
        return this;
    }
}
